import Vue from "vue";
import Vuex from "vuex";
import axios from "axios";
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    yu: [],
    io: JSON.parse(localStorage.getItem("op")) || [],
    ik: ["综合排序", "销量优先", "价格"],
    num: 0,
    num1: 0,
    // 声明一个布尔值用于判断
    los1: false,
    // arr1: [],
    arr: ["推荐", "母婴", "鞋包首饰", "食品", "数码家电", "居家百货"],
    hj: JSON.parse(localStorage.getItem("op")) || [],
    arr1: JSON.parse(localStorage.getItem("op1")) || [],
  },
  mutations: {
    // 请求数据
    git(state, h) {
      state.hj = h;
      console.log(state.hj);
    },
    // 点击推荐母婴使其数据发生改变
    dianji(state, q) {
      console.log(state, q);
      state.num = q;
      state.io = state.hj.filter((item) => item.type == q - 1);
      if (q == 0) {
        state.io = state.hj;
      }
    },
    // 点击使其销量排序
    dijia(state, v) {
      state.num1 = v;
      if (v == 0) {
        state.io.sort((a, b) => a.id - b.id);
        state.los1 = true;
      } else if (v == 1) {
        state.io.sort((a, b) => b.num - a.num);
        state.los1 = true;
      } else if (v == 2) {
        if (state.los1) {
          state.io.sort((a, b) => a.price - b.price);
          state.los1 = false;
        } else {
          state.io.sort((a, b) => b.price - a.price);
          state.los1 = true;
        }
      }
    },
    // 点击购物车页加加
    diop(state, o) {
      console.log(o);
      let items = state.arr1.find((n) => n.id == o.id);
      if (!items) {
        state.arr1.push(o);
      } else {
        state.arr1.map((item1) => {
          if (item1.id == o.id) {
            item1.shu = item1.shu + 1;
          }
        });
      }
      localStorage.setItem("op1", JSON.stringify(state.arr1));
    },
    // 点击按钮加加
    jia(state, k) {
      if (state.arr1[k].shu >= state.arr1[k].stock) {
        state.arr1[k].shu = state.arr1[k].stock;
      } else {
        state.arr1[k].shu++;
      }
      localStorage.setItem("op1", JSON.stringify(state.arr1));
    },
    // 点击按钮减减
    jian(state, i) {
      if (state.arr1[i].shu <= 1) {
        state.arr1[i].shu = 1;
      } else {
        state.arr1[i].shu--;
      }
      localStorage.setItem("op1", JSON.stringify(state.arr1));
    },
    // // 点击删除按钮
    shanchu1(state) {
      state.arr1 = state.arr1.filter((item) => {
        return item.done !== true;
      });
      localStorage.setItem("op1", JSON.stringify(state.arr1));
    },
  },
  actions: {
    // 获取数据
    async uis({ commit }) {
      let res = await axios.get("../mock.json");
      console.log(res);
      localStorage.setItem("op", JSON.stringify(res.data.data));
      commit("git", res.data.data);
    },
  },
  getters: {
    // 合计的钱数
    qianhe(state) {
      var dt = 0;
      state.arr1.forEach((item) => {
        if (item.done == true) {
          dt += item.price * item.shu;
        }
      });
      return dt;
    },
    // 计算删除的件数
    shanyi(state) {
      var g = 0;
      state.arr1.forEach((item) => {
        if (item.done == true) {
          g++;
        }
      });
      return g;
    },
  },
  modules: {},
});
